package com.ithaipeng.redanttakeaway.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithaipeng.redanttakeaway.dto.SetmealDto;
import com.ithaipeng.redanttakeaway.exception.CustomException;
import com.ithaipeng.redanttakeaway.pojo.Category;
import com.ithaipeng.redanttakeaway.pojo.Setmeal;
import com.ithaipeng.redanttakeaway.pojo.SetmealDish;
import com.ithaipeng.redanttakeaway.service.ICategoryService;
import com.ithaipeng.redanttakeaway.service.ISetmealDishService;
import com.ithaipeng.redanttakeaway.service.ISetmealService;
import com.ithaipeng.redanttakeaway.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private ISetmealService setmealService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private ISetmealDishService setmealDishService;

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody SetmealDto setmealDto){
        log.info("{}",setmealDto.toString());
        setmealService.insertWithDish(setmealDto);
        return Result.success("添加成功!");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page<SetmealDto>> page(@RequestParam(required = false,defaultValue = "1")Integer page,
                                        @RequestParam(required = false,defaultValue = "10")Integer pageSize,
                                        String name){
        Page<Setmeal> setmealPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(name!=null,Setmeal::getName,name);
        //添加排序条件
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //执行分页查询
        setmealService.page(setmealPage,setmealLambdaQueryWrapper);
        Page<SetmealDto> setmealDtoPage=new Page<>(page,pageSize);
        //对象拷贝
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> setmealDtoList = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                setmealDto.setCategoryName(category.getName());
                return setmealDto;
            } else {
                throw new CustomException("查询不到该套餐分类");
            }
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtoList);
        return Result.success(setmealDtoPage);
    }

    /**
     * 删除套餐!
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteWishDish(ids);
        return Result.success("删除成功!");
    }
    @GetMapping("/list")
    public Result<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
        List<Setmeal> setmealList = setmealService.list(setmealLambdaQueryWrapper);
        if (setmealList.isEmpty()){
            return Result.error("该套餐下没有套餐!");
        }else {
            return Result.success(setmealList);
        }
    }

    /**
     * 批量停售，起售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> status(@PathVariable("status")Integer status,@RequestParam("ids")List<Long> ids){
        log.info("status--->{}",status);
        log.info("list:{}",ids.toString());
        setmealService.statusUpDate(ids,status);
        return Result.success("操作成功!");
    }

    /**
     * 修改套餐，数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetmealDto> getById(@PathVariable("id") Long id){
        log.info("id-->{}",id);
        SetmealDto setmealDto=new SetmealDto();
        Setmeal setmeal = setmealService.getById(id);
        Long categoryId = setmeal.getCategoryId();
        Category category = categoryService.getById(categoryId);
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(id!=null,SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishLambdaQueryWrapper);
        if(category==null||setmealDishes.isEmpty()){
            throw new CustomException("该套餐出现了异常!");
        }
        setmealDto.setCategoryName(category.getName());
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);
        return Result.success(setmealDto);
        //return null;
    }

    /**
     * 保存修改的套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public Result<String> upDate(@RequestBody SetmealDto setmealDto){
        log.info("setmealDto--->{}",setmealDto.toString());
        setmealService.updateByDish(setmealDto);
        return Result.success("修改成功!");
    }
}
